
import os
import re

from types import UnionType

reverse_slash = re.compile(r"\\\\")
mult_slash = re.compile(r"/{2,}")


def file_path_join(paths: list) -> str:
    """将一个路径列表串连起来
        例如：
            paths = [
                "C:/",
                "Users",
                "Administrator",
                "Desktop",
                "test",
                "test.txt"
            ]  ==》 "C:/Users/Administrator/Desktop/test/test.txt"

            paths = [
                "C:/Users/Administrator/",
                "Desktop",
                "test",
                "test.txt"
            ]  ==》 "C:/Users/Administrator/Desktop/test/test.txt"

            paths = [
                "C:/Users/Administrator/",
                "/Desktop",
                "/test/",
                "/test.txt"
            ]  ==》 "C:/Users/Administrator/Desktop/test/test.txt"

    Returns:
        str: _description_
    """

    temp_path = "/".join(paths)
    temp_path = reverse_slash.sub(r"/", temp_path)

    return mult_slash.sub(r"/", temp_path)


def url_path_join(paths: list) -> str:

    temp_path = "/".join(paths)
    temp_path = reverse_slash.sub(r"/", temp_path)

    if "http" in temp_path:
        temp_path_startswith = temp_path[:10]
        temp_path_endswith = temp_path[10:]

    else:
        temp_path_startswith = ""
        temp_path_endswith = temp_path

    temp_path_endswith = mult_slash.sub(r"/", temp_path_endswith)
    temp_path = f"{temp_path_startswith}{temp_path_endswith}"
    return temp_path


def file_path_to_model_path(file_path: str) -> str:
    """将文件路径转换为模块路径
        例如：
            file_path = "/app/routers/routers_test/routers_test/"
            ==》 "app.routers.routers_test.routers_test"
    """

    file_path_list = re.split(r"[\/]", file_path)
    file_path = file_path_join(file_path_list)

    if file_path.startswith("/"):
        file_path = file_path[1:]

    if file_path.endswith("/"):
        file_path = file_path[:-1]

    return file_path.replace("/", ".")
